package com.jyy.prefabricated.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jyy.prefabricated.common.AjaxResult;
import com.jyy.prefabricated.dto.BatchDto;
import com.jyy.prefabricated.dto.Pageable;
import com.jyy.prefabricated.entity.NewsCategory;
import com.jyy.prefabricated.service.NewsCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 新闻分类
 * @version 1.0.0
 * @author: zjj
 */
@Slf4j
@RequestMapping("/api")
@RestController
public class NewsCategoryController {

    @Autowired
    private NewsCategoryService newsCategoryService;

    /**
     * 新增分类
     * @param newsCategory
     * @return
     */
    @PostMapping("/news-category")
    public AjaxResult insert(@Valid @RequestBody NewsCategory newsCategory){
        log.info("Rest request to insert newsCategory:{}",newsCategory);
        int id = newsCategoryService.insert(newsCategory);
        NewsCategory result = newsCategoryService.selectById(id);
        return AjaxResult.success(result);
    }

    /**
     * 更新分类
     * @param newsCategory
     * @return
     */
    @PutMapping("/news-category")
    public AjaxResult update(@Valid @RequestBody NewsCategory newsCategory){
        log.info("Rest request to update newsCategory:{}",newsCategory);
        newsCategoryService.update(newsCategory);
        NewsCategory result = newsCategoryService.selectById(newsCategory.getId());
        return AjaxResult.success(result);
    }

    /**
     * 批量更新状态
     * @param dto
     * @return
     */
    @PutMapping("/news-category-batch")
    public AjaxResult update(@Valid @RequestBody BatchDto dto){
        log.info("Rest request to batch-update newsCategory:{}",dto);
        newsCategoryService.batchUpdateStatus(dto);
        return AjaxResult.success();
    }

    /**
     * 批量删除
     * @param dto
     * @return
     */
    @DeleteMapping("/news-category-batch")
    public AjaxResult deleteById(@RequestBody BatchDto dto){
        log.info("Rest request to batch-delete newsCategory by id:{}",dto);
        int row = newsCategoryService.batchDelete(dto);
        if(row > 0){
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

    /**
     * 查询分类详情
     * @param id
     * @return
     */
    @GetMapping("/news-category/{id}")
    public AjaxResult selectById(@PathVariable Integer id){
        log.info("Rest request to find newsCategory by id:{}",id);
        NewsCategory result = newsCategoryService.selectById(id);
        return AjaxResult.success(result);
    }

    /**
     * 删除分类
     * @param id
     * @return
     */
    @DeleteMapping("/news-category/{id}")
    public AjaxResult deleteById(@PathVariable Integer id){
        log.info("Rest request to delete newsCategory by id:{}",id);
        int row = newsCategoryService.delete(id);
        if(row > 0){
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

    /**
     * 分页查询分类信息
     * @param category 名称
     * @param pid 父级id
     * @param status 状态
     * @param pageable 分页信息
     * @return
     */
    @GetMapping("/news-category")
    public AjaxResult selectAll(@RequestParam(required = false,defaultValue = "") String category,@RequestParam(required = false) Integer pid,@RequestParam(required = false) Integer status, Pageable pageable){
        log.info("Rest request to find newsCategory by category:{},pid:{},status:{}",category,pid,status);
        PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<NewsCategory> newsCategories = newsCategoryService.selectByParams(category, pid,status);
        PageInfo<NewsCategory> result = new PageInfo<>(newsCategories);
        return AjaxResult.success(result);
    }

    /**
     * 查询分类信息
     * @param category 名称
     * @param pid 父级id
     * @param status 状态
     * @return
     */
    @GetMapping("/news-category-list")
    public AjaxResult selectAll(@RequestParam(required = false,defaultValue = "") String category,@RequestParam(required = false) Integer pid, @RequestParam(required = false) Integer status){
        log.info("Rest request to find newsCategory by category:{},pid:{},status:{}",category,pid,status);
        List<NewsCategory> result = newsCategoryService.selectByParams(category, pid, status);
        return AjaxResult.success(result);
    }
}
